package com.hzhr.iot.user.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.user.sdk.constant.User_ErrorsEnum;
import com.hzhr.iot.user.sdk.model.dept.DeptDeleteModel;
import com.hzhr.iot.user.sdk.model.dept.DeptGetModel;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.model.dept.DeptPostModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class User_DeptService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 查询部门 <br/><br/>
     * 简介&nbsp;:&nbsp;查询部门 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询部门&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数查询部门(DEPT)； <br/>
     * 2.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数和查询到的结果组装响应并返回，响应模型的部分属性获取规则如下： <br/>
     * &nbsp;(1)&nbsp;“部门模型”对象deptModel_1.managementName的获取规则： <br/>
     * &nbsp;如果查询结果部门(DEPT)对象dept_1.level&nbsp;小于等于&nbsp;3，deptModel_1.managementName&nbsp;=&nbsp;dept_1.name， <br/>
     * &nbsp;否则，取出dept_1.code的前9位字符串str_1，并查询出部门(DEPT)dept_2，其中dept_2.code&nbsp;=&nbsp;str_1， <br/>
     * &nbsp;最后，deptModel_1.managementName&nbsp;=&nbsp;dept_2.name； <br/>
     * &nbsp;(2)&nbsp;“部门模型”对象deptModel_1.subCompanyName的获取规则： <br/>
     * &nbsp;如果查询结果部门(DEPT)对象dept_1.level&nbsp;小于等于&nbsp;2，deptModel_1.subCompanyName&nbsp;=&nbsp;dept_1.name， <br/>
     * &nbsp;否则，取出dept_1.code的前7位字符串str_1，并查询出部门(DEPT)dept_2，其中dept_2.code&nbsp;=&nbsp;str_1， <br/>
     * &nbsp;最后，deptModel_1.subCompanyName&nbsp;=&nbsp;dept_2.name； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_managementName : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示管理站名称;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_subCompanyName : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示分公司名称;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _is_paging : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.id = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _in_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.id in #{_in_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.name = #{_equal_name};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_companyId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.companyId = #{_equal_companyId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_level : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.level = #{_equal_level};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_parentId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.parentId = #{_equal_parentId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _like_resourceCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.resourceCode like #{_like_resourceCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _like_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.code like #{_like_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.code = #{_equal_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _lessthanorequal_level : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DEPT.level <= #{_lessthanorequal_level};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @throws ServiceException <br/>
     * @see DeptGetModel result <br/>
     */ 
    public DeptGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(null, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<DeptGetModel> result = restTemplate.exchange("http://user-service/depts?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.GET, httpEntity, DeptGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPTSERVICE_GET_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPTSERVICE_GET_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
    /**
     * 在登录用户的公司下删除非顶级部门 <br/><br/>
     * 简介&nbsp;:&nbsp;删除部门 <br/>
     * 术语&nbsp;: <br/>
     * 1.&nbsp;部门(DEPT)的直接下级部门： <br/>
     * &nbsp;满足以下条件的部门(DEPT)对象dept_1可以称作部门(DEPT)对象dept_2的直接下级部门： <br/>
     * &nbsp;(1)&nbsp;dept_1.parentId&nbsp;=&nbsp;dept_2； <br/>
     * 2.&nbsp;部门(DEPT)的下级用户： <br/>
     * &nbsp;满足以下条件的用户(USER)对象user_1可以称作部门(DEPT)对象dept_1的下级用户： <br/>
     * &nbsp;(1)&nbsp;user_1.dept_id&nbsp;=&nbsp;dept_1.id； <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询部门&nbsp;: <br/>
     * &nbsp;根据路径参数deptId查询部门(DEPT)dept_1，查询规则如下： <br/>
     * &nbsp;(1)&nbsp;dept_1.id&nbsp;=&nbsp;路径参数deptId； <br/>
     * &nbsp;如果dept_1不存在，抛出异常201； <br/>
     * 2.&nbsp;验证部门是否具备删除条件&nbsp;: <br/>
     * &nbsp;如果dept_1存在直接下级部门或下级用户，则抛出异常202； <br/>
     * 3.&nbsp;删除部门&nbsp;: <br/>
     * &nbsp;删除dept_1； <br/>
     * 4.&nbsp;返回信息&nbsp;: <br/>
     * &nbsp;生成“部门模型(删除)”对象result，并设置以下属性： <br/>
     * &nbsp;(1)&nbsp;result.changeCount&nbsp;=&nbsp;删除数量； <br/>
     * &nbsp;返回result； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param deptId 部门编号 <br/><br/>
     * @throws ServiceException <br/>
     * DEPTSERVICE_DELETE_BY_ID_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询部门<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 部门不存在<br/>
     * DEPTSERVICE_DELETE_BY_ID_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证部门是否具备删除条件<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 存在下级部门或下级用户，无法删除<br/>
     * @see DeptDeleteModel result <br/>
     */ 
    public DeptDeleteModel deleteById(Map<String, String> header, Integer deptId, Map<String, Object> query) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(null, headers);

        Map<String, Object> uriVariables = new HashMap<>();
        uriVariables.put("deptId", deptId);

        try {
            ResponseEntity<DeptDeleteModel> result = restTemplate.exchange("http://user-service/depts/{deptId}?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.DELETE, httpEntity, DeptDeleteModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPTSERVICE_DELETE_BY_ID_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPTSERVICE_DELETE_BY_ID_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
    /**
     * 在登录用户的公司下创建非顶级部门 <br/><br/>
     * 简介&nbsp;:&nbsp;在登录用户的公司下创建非顶级部门 <br/>
     * 术语&nbsp;: <br/>
     * 1.&nbsp;部门(DEPT)的直接下级部门： <br/>
     * &nbsp;满足以下条件的部门(DEPT)对象dept_1可以称作部门(DEPT)对象dept_2的直接下级部门： <br/>
     * &nbsp;(1)&nbsp;dept_1.parentId&nbsp;=&nbsp;dept_2； <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;验证上级部门编号&nbsp;: <br/>
     * &nbsp;查询部门(DEPT)对象dept_1，查询规则如下： <br/>
     * &nbsp;(1)&nbsp;dept_1.id&nbsp;=&nbsp;body.parentId； <br/>
     * &nbsp;(2)&nbsp;dept_1.companyId&nbsp;=&nbsp;登录用户.companyId； <br/>
     * &nbsp;如果dept_1不存在，抛出异常201； <br/>
     * 2.&nbsp;验证部门名称&nbsp;: <br/>
     * &nbsp;如果存在部门(DEPT)对象dept满足以下条件，则抛出异常202： <br/>
     * &nbsp;(1)&nbsp;dept.name&nbsp;=&nbsp;body.name <br/>
     * 3.&nbsp;创建部门&nbsp;: <br/>
     * &nbsp;根据body创建部门(DEPT)对象dept_2，并设置以下属性： <br/>
     * &nbsp;(1)&nbsp;dept_2.code的设置规则： <br/>
     * &nbsp;如果dept_1不存在直接下级部门，那么：dept_2.code&nbsp;=&nbsp;dept_1.code&nbsp;+&nbsp;'01'； <br/>
     * &nbsp;如果dept_1存在直接下级部门，查询出dept_1的直接下级部门中code值最大的部门(DEPT)对象，提取出该对象的code的最后两位，转换为数字并在此基础上&nbsp;+1&nbsp;， <br/>
     * &nbsp;然后左补零至两位字符串str_1，最后：dept_2.code&nbsp;=&nbsp;dept_1.code&nbsp;+&nbsp;str_1； <br/>
     * &nbsp;(2)&nbsp;dept_2.resourceCode的设置规则： <br/>
     * &nbsp;如果dept_1不存在直接下级部门，那么：dept_2.resourceCode&nbsp;=&nbsp;dept_1.resourceCode&nbsp;+&nbsp;'01'； <br/>
     * &nbsp;如果dept_1存在直接下级部门，查询出dept_1的直接下级部门中resourceCode值最大的部门(DEPT)对象，提取出该对象的resourceCode的最后两位，转换为数字并在此基础上&nbsp;+1&nbsp;， <br/>
     * &nbsp;然后左补零至两位字符串str_1，最后：dept_2.resourceCode&nbsp;=&nbsp;dept_1.resourceCode&nbsp;+&nbsp;str_1； <br/>
     * &nbsp;(3)&nbsp;dept_2.level&nbsp;=&nbsp;dept_1.level&nbsp;+&nbsp;1； <br/>
     * &nbsp;(4)&nbsp;dept_2.parentName&nbsp;=&nbsp;dept_1.name； <br/>
     * &nbsp;(5)&nbsp;dept_2.companyId&nbsp;=&nbsp;dept_1.companyId； <br/>
     * 4.&nbsp;返回信息&nbsp;: <br/>
     * &nbsp;调用“查询部门”服务(user-service:&nbsp;/depts&nbsp;GET)获取“部门模型(查询)”对象result，调用规则如下： <br/>
     * &nbsp;(1)&nbsp;accessMode:&nbsp;GLOBAL <br/>
     * &nbsp;(2)&nbsp;query:&nbsp;_equal_id&nbsp;=&nbsp;dept_2.id <br/>
     * &nbsp;设置result属性，设置规则如下： <br/>
     * &nbsp;(1)&nbsp;result.changeCount&nbsp;=&nbsp;创建数量； <br/>
     * &nbsp;返回result； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * DEPTSERVICE_POST_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证上级部门<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 上级部门不存在<br/>
     * DEPTSERVICE_POST_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证部门名称<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 部门名称已存在<br/>
     * @see DeptPostModel body <br/>
     * @see DeptGetModel result <br/>
     */ 
    public DeptGetModel post(Map<String, String> header, Map<String, Object> query, DeptPostModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<DeptGetModel> result = restTemplate.exchange("http://user-service/depts?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.POST, httpEntity, DeptGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPTSERVICE_POST_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPTSERVICE_POST_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

